package com.quanquan.management.controller;

import DTO.PageDTO;
import base.BaseConst;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.quanquan.management.dto.SysRoleDTO;
import com.quanquan.management.entity.SysRole;
import com.quanquan.management.request.SysRoleRequestBody;
import com.quanquan.management.service.SysPermissionRoleService;
import com.quanquan.management.service.SysPermissionService;
import com.quanquan.management.service.SysRoleService;
import enums.ResCodeEnum;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import request.PageRequestBody;
import web.response.ResultBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @description:
 * @author:gongzhou
 * @date:2019-04-29 13:30
 * @version:v 1.0.0
 */
@Log4j2
@Controller
@RequestMapping("/sys")
public class SysAuthController extends ManagerBaseController {

    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 添加系统角色
     *
     * @param requestBody
     * @return
     */
    @RequestMapping("/role/add")
    public String add(SysRoleRequestBody requestBody) {


        if (BaseConst.RequestMethod.GET.equals(this.request.getMethod())) {
            return "sysRole/form";
        }
            boolean b = sysRoleService.addRole(requestBody);
            if (b) {
                return "redirect:/sys/role/list";
            }
            return null;

    }

    /**
     * 角色列表
     *
     * @param
     * @param model
     * @return
     */
    @GetMapping("/role/list")
    public String list(@RequestParam(required = false) Integer pageSize,
                       @RequestParam(required = false) Integer page,
                       @RequestParam(required = false) String keyword,
                       @RequestParam(required = false) String sort,
                       @RequestParam(required = false) boolean isAsc, Model model ) {
        if (null == pageSize) {
            pageSize = 10;
        }
        if (null == page) {
            page = 0;
        } else if (page > 0){
            page = page -1;
        }
        PageRequestBody requestBody = new PageRequestBody(page, pageSize, keyword, sort, isAsc);
        PageDTO<SysRoleDTO> list = sysRoleService.list(requestBody);
        model.addAttribute("pageObj", list);
        return "sysRole/list";
    }

    @PostMapping("/role/query")
    @ResponseBody
    public ResultBody findAllRole(){
        try {
            List<SysRole> all = sysRoleService.findAll();
            List<SysRoleDTO> allDto = new ArrayList<>();
            all.forEach(s -> {
                SysRoleDTO dto = new SysRoleDTO();
                BeanUtil.copyProperties(s, dto, true, CopyOptions.create().setIgnoreNullValue(true).setIgnoreProperties("regTime", "updateTime"));
                allDto.add(dto);
            });
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), allDto);
        } catch (Exception e) {
            log.error(e.getMessage());
            return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg());
        }
    }

    @GetMapping("/role/getAll")
    @ResponseBody
    public ResultBody getRoles() {
        try {
            List<SysRole> all = sysRoleService.findAll();
            List<SysRoleDTO> allDto = new ArrayList<>();
            all.forEach(s -> {
                SysRoleDTO dto = new SysRoleDTO();
                BeanUtil.copyProperties(s, dto, true, CopyOptions.create().setIgnoreNullValue(true).setIgnoreProperties("regTime", "updateTime"));
                allDto.add(dto);
            });
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), allDto);
        } catch (Exception e) {
            log.error(e.getMessage());
            return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg());
        }
    }


    /**
     * 编辑
     * @param id
     * @param requestBody
     * @param model
     * @return
     */
    @RequestMapping("/edit")
    public String edit(String id, SysRoleRequestBody requestBody, Model model) {

        if (BaseConst.RequestMethod.GET.equals(this.request.getMethod())) {
            SysRoleDTO byId = sysRoleService.findById(id);
            model.addAttribute("editable",byId);
            return "sysRole/form";
        }
        boolean update = sysRoleService.update(requestBody);
        if (update) {
            return "redirect:/sys/role/list";
        } else {
            Map<String, Object> result = new HashMap<>();
            result.put("code",-1);
            result.put("msg","修改失败");
            model.addAllAttributes(result);
            return null;
        }
    }


    @GetMapping("/delete")
    public String delete(@RequestParam String id,Model model) {
        boolean delete = sysRoleService.delete(id);
        if (delete) {
            return "redirect:/sys/role/list";
        } else {
            model.addAttribute("code", -1);
        }
        return null;
    }
}
